<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/9/29 0029
 * Time: 下午 5:54
 */

namespace common\widgets\pagination;


use yii\widgets\LinkPager;
use yii\helpers\Html;
use yii\helpers\Url;

class AjaxLinkPager extends LinkPager
{
    public $ser_filter;
    public $js;

    public function renderPageButton($label, $page, $class, $disabled, $active)
    {
        $options = [
            'class' => empty($class) ? $this->pageCssClass : $class,
            'url' => $this->pagination->createFilterUrl($page,$this->ser_filter),
        ];
        if ($active) {
            Html::addCssClass($options, $this->activePageCssClass);
        }
        if ($disabled) {
            Html::addCssClass($options, $this->disabledPageCssClass);

            return Html::tag('li', Html::tag('span', $label), $options);
        }
        $linkOptions = $this->linkOptions;
        $linkOptions['data-page'] = $page;

        return Html::tag('li', Html::a($label, 'javascript:;', $linkOptions), $options);
    }

    public function run()
    {
        parent::run();
        $ul = ob_get_contents();
        ob_clean();
        PaginationAsset::register($this->view);
        $js = '
        $(\'.list .pagination\').on(\'click\', \'li\', function() {
            var pagination = new Pagination();
            pagination.pagination($(this));
        });
        $(".ListFilterForm").on("click", ".submit", function() {
            var pagination = new Pagination();
            pagination.listFilterFormSubmit($(this));
        });
        $(\'body\').on(\'keyup\', \'.ListFilterForm input\',function(event) {
            if (event.keyCode != "13") {
                return ;
            }
            event.preventDefault();
            var container = $(this).parents(\'form\');
            container.find(\'.submit\').click();
            return false;
        });
        $(\'body\').on(\'click\', \'.pagination-area form .submit\',function(event) {
            var pagination = new Pagination();
            pagination.skipForm($(this));
        });
        $(\'body\').on(\'keyup\', \'.pagination-area form input\',function(event) {
            if (event.keyCode != "13") {
                return ;
            }
            event.preventDefault();
            var container = $(this).parents(\'form\');
            container.find(\'.submit\').click();
            return false;
        });
        ' . $this->js;
        $this->view->registerJs($js);
        if($this->pagination->getPageCount() > 1) {
            return "
            <div class='pagination-area clearfix'>
                <div class='skip-operate fr'>
                <span class='total-num'>共".$this->pagination->getPageCount()."页</span>
                <form style='display:inline-block' method='get' action='".Url::to([
                        $this->pagination->route
                    ])."'>
                <input type='hidden' name='ser_filter' value='".$this->ser_filter."'>
                <input type='text' class='display-none' name='per-page' value='".$this->pagination->pageSize."'>
                跳转到:<input type='text' class='form-control page-num' name='page'> 页.
                <button class='btn btn-primary submit btn-skip' type='button'>跳转</button>
                </form>
                </div>
                ".$ul."
                <div class='loading_box'>
                <div class='loading'></div>
                <div class='text'>LOADING</div>
                </div>
            </div>
            ";
        }

        return "
            <div class='pagination-area'>
                ".$ul."
                <div class='loading_box'>
                <div class='loading'></div>
                <div class='text'>LOADING</div>
                </div>
            </div>
            ";
    }

    public static function beginSearchForm($action, $method = 'post', $options = null) {
        $options = $options?$options:['class' => 'form-horizontal ListFilterForm'];
        return  Html::beginForm($action, $method, $options);
    }

    public static function endSearchForm() {
       return Html::endForm();
    }
}